package com.cn.mmd.controller;

import com.cn.mmd.model.StudentApp;
import com.cn.mmd.model.SuggestionFeedback;
import com.cn.mmd.service.StudentAppService;
import com.cn.mmd.service.SuggestionFeedbackService;
import com.cn.mmd.service.TeacherDataLService;
import com.cn.mmd.tools.AliyunOssUtil;
import com.cn.mmd.tools.PictureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 意见反馈
 * Created by Administrator on 2016/9/5 0005.
 */

@Controller
@RequestMapping(value = "page/suggestionFeedback")
public class SuggestionFeedbackController {

    @Autowired
    private StudentAppService studentAppService;

    @Autowired
    private TeacherDataLService teacherDataLService;

    @Autowired
    private SuggestionFeedbackService suggestionFeedbackService;

    @Autowired
    private TokenController tokenController;

    @RequestMapping(value = "/add")//添加意见反馈
    @ResponseBody
    public synchronized Object manageCourseAdd(HttpServletRequest request, @RequestParam(value = "files", required = false) MultipartFile[] files) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("feedbackId");
        String feedbackInformation = request.getParameter("feedbackInformation");
        String feedbackUr = "";
        String token = request.getParameter("token");
        String type = request.getParameter("type");
        Object studentApp = null;
        if (type.equals("1")) {
            studentApp = tokenController.token1(studentId, token);
        } else if (type.equals("2")) {
            studentApp = tokenController.token(studentId, token);
        }
        if (studentApp != null) {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String feedbackTime = simpleDateFormat.format(date);
            SuggestionFeedback suggestionFeedback = new SuggestionFeedback();
            suggestionFeedback.setFeedbackInformation(feedbackInformation);
            suggestionFeedback.setStudentId(studentId);
            suggestionFeedback.setFeedbackTime(feedbackTime);
            if (files == null) {
                suggestionFeedback.setFeedbackUrl(feedbackUr);
            } else {
                if (files.length <= 5) {
                    MultipartFile file;
                    String url = "";
                    String path = request.getSession().getServletContext().getRealPath("/") + "upload";
                    File targetFile;
                    for (int i = 0; i < files.length; i++) {
                        file = files[i];
                        if (!file.getOriginalFilename().equals("")) {
                            String key = studentId + "-feedbackInformation-" + file.getOriginalFilename() + feedbackInformation;
                            targetFile = new File(path, key);
                            if (!targetFile.exists()) {
                                targetFile.mkdirs();
                                try {
                                    file.transferTo(targetFile);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                targetFile.delete();
                                try {
                                    file.transferTo(targetFile);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            PictureUtil pictureUtil = new PictureUtil();
                            pictureUtil.zipWidthHeightImageFile(targetFile.getPath(), 720, 360, 4);
                            feedbackUr = AliyunOssUtil.save(key, targetFile.getPath());
                            if (i < files.length - 1) {
                                url = feedbackUr + "," + url;
                            } else {
                                url = url + feedbackUr;
                            }
                            targetFile.delete();
                        }
                    }
                    suggestionFeedback.setFeedbackUrl(url);
                }else {
                    objectMap.put("code", 202);
                    objectMap.put("message", "添加失败图片数量不对");
                }
                }
                if (suggestionFeedbackService.insertSelective(suggestionFeedback) > 0) {
                    objectMap.put("code", 200);
                    objectMap.put("message", "成功");
                } else {
                    objectMap.put("code", 201);
                    objectMap.put("message", "添加失败");
                }
        } else {
            objectMap.put("code", 203);
            objectMap.put("message", "此用户没有权限做此操作");
        }
        return objectMap;
    }
}
